import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class QuestionWindow extends JFrame {

	private JTextArea questionText;
	private JTextArea usersText;
	private GuiClient gui;
	private int ownerID;
	
	public QuestionWindow(GuiClient gui) {
		super("create question");
		
		this.gui = gui;
		
		String st = JOptionPane.showInputDialog("Type in owner ID (integer) for the questionnaire");
		if(st==null){
			System.exit(0);
		}
		ownerID = Integer.parseInt(st);
		
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.ipadx = 10;
        c.ipady = 10;
        c.insets = new Insets(5,5,5,5);
        c.fill = GridBagConstraints.NONE;
		
        c.gridx = 0;
		c.gridy = 0;
		add(new JLabel("Questions"), c);
        
		questionText = new JTextArea();
		questionText.setPreferredSize(new Dimension(400,200));
		questionText.setLineWrap(true);
		questionText.setWrapStyleWord(true);
		JScrollPane qScroll = new JScrollPane(questionText);
		c.gridx = 0;
		c.gridy = 1;
		add(qScroll, c);
		
		c.gridx = 0;
		c.gridy = 2;
		add(new JLabel("User IDs"), c);
		
		usersText = new JTextArea();
		usersText.setPreferredSize(new Dimension(400,150));
		usersText.setLineWrap(true);
		usersText.setWrapStyleWord(true);
		JScrollPane uScroll = new JScrollPane(usersText);
		c.gridx = 0;
		c.gridy = 3;
		add(uScroll, c);
		
		QSaveButton button = new QSaveButton(this);
		c.gridx = 0;
		c.gridy = 4;
		add(button, c);
		
		pack();
		setVisible(true);
		
	}
	
	public void saveQuestion() {
		//Question q = new Question(questionText.getText());
		String[] s = questionText.getText().split("\n\n");
		String[] s2 = usersText.getText().split("\n");
		ArrayList<Question> questions = new ArrayList<Question>();
		ArrayList<Integer> users = new ArrayList<Integer>();
		for(int i=0; i<s.length; i++){
			questions.add(new Question(s[i].trim()));
		}
		for(int i=0; i<s2.length; i++){
			users.add(Integer.parseInt(s2[i].trim()));
		}
		Questionnaire q = new Questionnaire(ownerID, questions, users);
		gui.save(q, null);
	}
}
